<?php
/**
 * Created by PhpStorm.
 * User: niechenguang
 * Date: 2016-12-19
 * Time: 0:02
 */
namespace notification;

use app\api\controller\Common;
use app\library\CommonConfig;
use think\Log;
use think\Exception;

require_once(dirname(__FILE__) . '/' . 'android/AndroidBroadcast.php');
require_once(dirname(__FILE__) . '/' . 'android/AndroidFilecast.php');
require_once(dirname(__FILE__) . '/' . 'android/AndroidGroupcast.php');
require_once(dirname(__FILE__) . '/' . 'android/AndroidUnicast.php');
require_once(dirname(__FILE__) . '/' . 'android/AndroidCustomizedcast.php');
require_once(dirname(__FILE__) . '/' . 'ios/IOSBroadcast.php');
require_once(dirname(__FILE__) . '/' . 'ios/IOSFilecast.php');
require_once(dirname(__FILE__) . '/' . 'ios/IOSGroupcast.php');
require_once(dirname(__FILE__) . '/' . 'ios/IOSUnicast.php');
require_once(dirname(__FILE__) . '/' . 'ios/IOSCustomizedcast.php');

class Notification
{
    protected $appkey = NULL;
    protected $appMasterSecret = NULL;
    protected $timestamp = NULL;
    protected $validation_token = NULL;

    function __construct()
    {
        $this->appkey = CommonConfig::NOTIFICATION_APPKEY;
        $this->appMasterSecret = CommonConfig::NOTIFICATION_APPMASTERSECRET;
        $this->timestamp = strval(time());
    }

    function sendAndroidBroadcast()
    {
        try {
            $brocast = new \AndroidBroadcast();
            $brocast->setAppMasterSecret($this->appMasterSecret);
            $brocast->setPredefinedKeyValue("appkey", $this->appkey);
            $brocast->setPredefinedKeyValue("timestamp", $this->timestamp);
            $brocast->setPredefinedKeyValue("ticker", "Android broadcast ticker");
            $brocast->setPredefinedKeyValue("title", "中文的title");
            $brocast->setPredefinedKeyValue("text", "Android broadcast text");
            $brocast->setPredefinedKeyValue("after_open", "go_app");
            // Set 'production_mode' to 'false' if it's a test device. 
            // For how to register a test device, please see the developer doc.
            $brocast->setPredefinedKeyValue("production_mode", "true");
            // [optional]Set extra fields
            $brocast->setExtraField("test", "helloworld");
            print("Sending broadcast notification, please wait...\r\n");
            $brocast->send();
            print("Sent SUCCESS\r\n");
        } catch (Exception $e) {
            print("Caught exception: " . $e->getMessage());
        }
    }

    function sendAndroidUnicast()
    {
        try {
            $unicast = new \AndroidUnicast();
            $unicast->setAppMasterSecret($this->appMasterSecret);
            $unicast->setPredefinedKeyValue("appkey", $this->appkey);
            $unicast->setPredefinedKeyValue("timestamp", $this->timestamp);
            // Set your device tokens here
            $unicast->setPredefinedKeyValue("device_tokens", "xx");
            $unicast->setPredefinedKeyValue("ticker", "Android unicast ticker");
            $unicast->setPredefinedKeyValue("title", "Android unicast title");
            $unicast->setPredefinedKeyValue("text", "Android unicast text");
            $unicast->setPredefinedKeyValue("after_open", "go_app");
            // Set 'production_mode' to 'false' if it's a test device. 
            // For how to register a test device, please see the developer doc.
            $unicast->setPredefinedKeyValue("production_mode", "true");
            // Set extra fields
            $unicast->setExtraField("test", "helloworld");
            print("Sending unicast notification, please wait...\r\n");
            $unicast->send();
            print("Sent SUCCESS\r\n");
        } catch (Exception $e) {
            print("Caught exception: " . $e->getMessage());
        }
    }

    function sendAndroidFilecast()
    {
        try {
            $filecast = new \AndroidFilecast();
            $filecast->setAppMasterSecret($this->appMasterSecret);
            $filecast->setPredefinedKeyValue("appkey", $this->appkey);
            $filecast->setPredefinedKeyValue("timestamp", $this->timestamp);
            $filecast->setPredefinedKeyValue("ticker", "Android filecast ticker");
            $filecast->setPredefinedKeyValue("title", "Android filecast title");
            $filecast->setPredefinedKeyValue("text", "Android filecast text");
            $filecast->setPredefinedKeyValue("after_open", "go_app");  //go to app
            print("Uploading file contents, please wait...\r\n");
            // Upload your device tokens, and use '\n' to split them if there are multiple tokens
            $filecast->uploadContents("aa" . "\n" . "bb");
            print("Sending filecast notification, please wait...\r\n");
            $filecast->send();
            print("Sent SUCCESS\r\n");
        } catch (Exception $e) {
            print("Caught exception: " . $e->getMessage());
        }
    }

    function sendAndroidGroupcast()
    {
        try {
            /* 
              *  Construct the filter condition:
              *  "where": 
              *	{
              *		"and": 
              *		[
                *			{"tag":"test"},
                *			{"tag":"Test"}
              *		]
              *	}
              */
            $filter = array(
                "where" => array(
                    "and" => array(
                        array(
                            "tag" => "test"
                        ),
                        array(
                            "tag" => "Test"
                        )
                    )
                )
            );

            $groupcast = new \AndroidGroupcast();
            $groupcast->setAppMasterSecret($this->appMasterSecret);
            $groupcast->setPredefinedKeyValue("appkey", $this->appkey);
            $groupcast->setPredefinedKeyValue("timestamp", $this->timestamp);
            // Set the filter condition
            $groupcast->setPredefinedKeyValue("filter", $filter);
            $groupcast->setPredefinedKeyValue("ticker", "Android groupcast ticker");
            $groupcast->setPredefinedKeyValue("title", "Android groupcast title");
            $groupcast->setPredefinedKeyValue("text", "Android groupcast text");
            $groupcast->setPredefinedKeyValue("after_open", "go_app");
            // Set 'production_mode' to 'false' if it's a test device. 
            // For how to register a test device, please see the developer doc.
            $groupcast->setPredefinedKeyValue("production_mode", "true");
            print("Sending groupcast notification, please wait...\r\n");
            $groupcast->send();
            print("Sent SUCCESS\r\n");
        } catch (Exception $e) {
            print("Caught exception: " . $e->getMessage());
        }
    }

    function sendAndroidCustomizedcast()
    {
        try {
            $customizedcast = new \AndroidCustomizedcast();
            $customizedcast->setAppMasterSecret($this->appMasterSecret);
            $customizedcast->setPredefinedKeyValue("appkey", $this->appkey);
            $customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);
            // Set your alias here, and use comma to split them if there are multiple alias.
            // And if you have many alias, you can also upload a file containing these alias, then 
            // use file_id to send customized notification.
            $customizedcast->setPredefinedKeyValue("alias", "xx");
            // Set your alias_type here
            $customizedcast->setPredefinedKeyValue("alias_type", "xx");
            $customizedcast->setPredefinedKeyValue("ticker", "Android customizedcast ticker");
            $customizedcast->setPredefinedKeyValue("title", "Android customizedcast title");
            $customizedcast->setPredefinedKeyValue("text", "Android customizedcast text");
            $customizedcast->setPredefinedKeyValue("after_open", "go_app");
            print("Sending customizedcast notification, please wait...\r\n");
            $customizedcast->send();
            print("Sent SUCCESS\r\n");
        } catch (Exception $e) {
            print("Caught exception: " . $e->getMessage());
        }
    }

    function sendAndroidCustomizedcastFileId()
    {
        try {
            $customizedcast = new \AndroidCustomizedcast();
            $customizedcast->setAppMasterSecret($this->appMasterSecret);
            $customizedcast->setPredefinedKeyValue("appkey", $this->appkey);
            $customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);
            // if you have many alias, you can also upload a file containing these alias, then
            // use file_id to send customized notification.
            $customizedcast->uploadContents("aa" . "\n" . "bb");
            // Set your alias_type here
            $customizedcast->setPredefinedKeyValue("alias_type", "xx");
            $customizedcast->setPredefinedKeyValue("ticker", "Android customizedcast ticker");
            $customizedcast->setPredefinedKeyValue("title", "Android customizedcast title");
            $customizedcast->setPredefinedKeyValue("text", "Android customizedcast text");
            $customizedcast->setPredefinedKeyValue("after_open", "go_app");
            print("Sending customizedcast notification, please wait...\r\n");
            $customizedcast->send();
            print("Sent SUCCESS\r\n");
        } catch (Exception $e) {
            print("Caught exception: " . $e->getMessage());
        }
    }

    function sendIOSBroadcast($alert, $arrCustomField = [], $production_mode = false)
    {
        try {
            $brocast = new \IOSBroadcast();
            $brocast->setAppMasterSecret($this->appMasterSecret);
            $brocast->setPredefinedKeyValue("appkey", $this->appkey);
            $brocast->setPredefinedKeyValue("timestamp", $this->timestamp);

            $brocast->setPredefinedKeyValue("alert", $alert);
            $brocast->setPredefinedKeyValue("badge", 0);
            $brocast->setPredefinedKeyValue("sound", "chime");
            $brocast->setPredefinedKeyValue("production_mode", $production_mode);
            foreach ($arrCustomField as $key => $val) {
                $brocast->setCustomizedField($key, $val);
            }
            $brocast->send();
            return true;
        } catch (Exception $e) {
            Log::error(' send ios broadcast fail.[errmsg: ' . $e->getMessage() . ']');
            return false;
        }
    }

    function sendIOSUnicast($device_token, $alert, $arrCustomField = [], $production_mode = false)
    {
        try {
            $unicast = new \IOSUnicast();
            $unicast->setAppMasterSecret($this->appMasterSecret);
            $unicast->setPredefinedKeyValue("appkey", $this->appkey);
            $unicast->setPredefinedKeyValue("timestamp", $this->timestamp);

            $unicast->setPredefinedKeyValue("device_tokens", $device_token);
            $unicast->setPredefinedKeyValue("alert", $alert);
            $unicast->setPredefinedKeyValue("badge", 0);
            $unicast->setPredefinedKeyValue("sound", "chime");

            $unicast->setPredefinedKeyValue("production_mode", $production_mode);

            foreach ($arrCustomField as $key => $val) {
                $unicast->setCustomizedField($key, $val);
            }

            $unicast->send();
            return true;
        } catch (Exception $e) {
            Log::error(' send ios broadcast fail.[errmsg: ' . $e->getMessage() . ']');
            return false;
        }
    }

    /**
     *
     * @param $device_tokens  \n分隔的设备号字符串
     * @param $alert
     * @param bool $production_mode
     * @return bool
     * @throws \Exception
     */
    function sendIOSFilecast($device_tokens, $alert, $production_mode = false)
    {
        try {
            $filecast = new \IOSFilecast();
            $filecast->setAppMasterSecret($this->appMasterSecret);
            $filecast->setPredefinedKeyValue("appkey", $this->appkey);
            $filecast->setPredefinedKeyValue("timestamp", $this->timestamp);

            $filecast->setPredefinedKeyValue("alert", $alert);
            $filecast->setPredefinedKeyValue("badge", 0);
            $filecast->setPredefinedKeyValue("sound", "chime");

            $filecast->setPredefinedKeyValue("production_mode", $production_mode);
            $filecast->uploadContents($device_tokens);
            $filecast->send();
            return true;
        } catch (Exception $e) {
            Log::error(' send ios broadcast fail.[errmsg: ' . $e->getMessage() . ']');
            return false;
        }
    }

    function sendIOSGroupcast($filter, $alert, $production_mode = false)
    {
        try {
            /* 
              *  Construct the filter condition:
              *  "where": 
              *	{
              *		"and": 
              *		[
                *			{"tag":"iostest"}
              *		]
              *	}
              */
//            $filter = 	array(
//                "where" => 	array(
//                    "and" 	=>  array(
//                        array(
//                            "tag" => "iostest"
//                        )
//                    )
//                )
//            );

            $groupcast = new \IOSGroupcast();
            $groupcast->setAppMasterSecret($this->appMasterSecret);
            $groupcast->setPredefinedKeyValue("appkey", $this->appkey);
            $groupcast->setPredefinedKeyValue("timestamp", $this->timestamp);
            // Set the filter condition
            $groupcast->setPredefinedKeyValue("filter", $filter);
            $groupcast->setPredefinedKeyValue("alert", $alert);
            $groupcast->setPredefinedKeyValue("badge", 0);
            $groupcast->setPredefinedKeyValue("sound", "chime");

            $groupcast->setPredefinedKeyValue("production_mode", $production_mode);
            $groupcast->send();
            return true;
        } catch (Exception $e) {
            Log::error(' send ios broadcast fail.[errmsg: ' . $e->getMessage() . ']');
            return false;
        }
    }

    function sendIOSCustomizedcast($alias, $alias_type, $alert, $production_mode = false)
    {
        try {
            $customizedcast = new \IOSCustomizedcast();
            $customizedcast->setAppMasterSecret($this->appMasterSecret);
            $customizedcast->setPredefinedKeyValue("appkey", $this->appkey);
            $customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);

            // Set your alias here, and use comma to split them if there are multiple alias.
            // And if you have many alias, you can also upload a file containing these alias, then 
            // use file_id to send customized notification.
            $customizedcast->setPredefinedKeyValue("alias", $alias);
            // Set your alias_type here
            $customizedcast->setPredefinedKeyValue("alias_type", $alias_type);
            $customizedcast->setPredefinedKeyValue("alert", $alert);
            $customizedcast->setPredefinedKeyValue("badge", 0);
            $customizedcast->setPredefinedKeyValue("sound", "chime");

            $customizedcast->setPredefinedKeyValue("production_mode", $production_mode);
            $customizedcast->send();
            return true;
        } catch (Exception $e) {
            Log::error(' send ios broadcast fail.[errmsg: ' . $e->getMessage() . ']');
            return false;
        }
    }
}